#include "mcontinuityform.h"
#include "ui_mcontinuityform.h"
#include<QTimer>
#include<QStringList>
#include<QTableWidget>
#include<QtMath>
#include"data.h"

MContinuityForm::MContinuityForm(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::MContinuityForm)
{
    ui->setupUi(this);
    connect(ui->pushButton_start,&QPushButton::clicked,this,[=](){on_Button_start_clicked();});
    connect(ui->pushButton_stop,&QPushButton::clicked,this,[=](){on_PushButton_stop_clicked();});
    connect(ui->pushButton_cleanpos,&QPushButton::clicked,this,[=](){on_PushButton_cleanpos_clicked();});
    connect(ui->pushButton_addpoint,&QPushButton::clicked,this,[=](){on_addPoint_clicked();});
    connect(ui->pushButton_removepoints,&QPushButton::clicked,this,[=](){on_removePoint_clicked();});

    QTimer *timer1=new QTimer(this);
    connect(timer1,&QTimer::timeout,this,&MContinuityForm::updateWin);
    timer1->start(100);


}

MContinuityForm::~MContinuityForm()
{
    delete ui;
}

//启动
void MContinuityForm::on_Button_start_clicked(){
    if(Data::handle!=nullptr){
        for(int i=0;i<4;++i){
            int enable=0;
            PMC_GetAxisEnable(Data::handle,i,&enable);
            if(enable==0){
                QMessageBox::about(NULL,"Warn","请检查AXIS0-3使能情况！！！");
                return;
            }
        }
        ui->pushButton_start->setEnabled(false);
        ui->label_flag->setText("运行");
        ui->label_flag->setStyleSheet("background-color: rgb(0,0,255)");
        runflage=true;
        float speed=ui->spinBox_sp->value();
        float accel=ui->spinBox_acc->value();
        float decel=ui->spinBox_del->value();
        int changeMerge=ui->checkBox_contm->isChecked()?1:0;    //连续插补
        int changesp=ui->checkBox_changesp->isChecked()?1:0;    //变速
        //int isdev=ui->checkBox_isdev->isChecked()?1:0;          //自动拐角减速
        //int islimit=ui->checkBox_islimit->isChecked()?1:0;      //自动小圆限速
        //int isdaojiao=ui->checkBox_isdaojiao->isChecked()?1:0;  //自动倒角
        float limitsp=ui->spinBox_limitsp->value();             //限制速度
        float startdev=ui->spinBox_startangle->value();         //起始减速角度
        float stopdev=ui->spinBox_stopangle->value();           //停止减速角度
        float limitrad=ui->spinBox_cirrad->value();             //小圆限速半径
        float daojiaorad=ui->spinBox_chamferrad->value();       //倒角半径

        PMC_Base(Data::handle,4,axislist);
        for(int i=0;i<4;++i){
            PMC_SetAxisEnable(Data::handle,axislist[i],1);
            PMC_SetAtype(Data::handle,axislist[i],1);
            PMC_SetUnits(Data::handle,axislist[i],200);
            PMC_SetSpeed(Data::handle,axislist[i],speed);
            PMC_SetAccel(Data::handle,axislist[i],accel);
            PMC_SetDecel(Data::handle,axislist[i],decel);
            PMC_SetMerge(Data::handle, axislist[i], changeMerge);
            PMC_SetSpeedDyna(Data::handle, axislist[i], changesp);
        }

        int cmode=0;
        if (ui->checkBox_isdev->isChecked())
        {
            cmode += 2;
            PMC_SetDecelAngle(Data::handle,0,(float)(startdev*M_PI/180.0));
            PMC_SetStopAngle(Data::handle,0, (float)(stopdev * M_PI / 180.0));
        }
        if (ui->checkBox_islimit->isChecked()) {
            cmode += 8;

            PMC_SetFullSpRadius(Data::handle,0,limitrad);
        }
        if (ui->checkBox_isdaojiao->isChecked())
        {
            cmode += 32;
            PMC_SetFullSpRadius(Data::handle, 0, daojiaorad);
        }

        PMC_SetCornerMode(Data::handle, 0, cmode);
        PMC_SetForceSpeed(Data::handle, 0, limitsp);

        for(int i=0;i<=pointrow;++i){
            float xpoint=ui->tableWidge_points->item(i,0)->text().toFloat();
            float ypoint=ui->tableWidge_points->item(i,1)->text().toFloat();
            float zpoint=ui->tableWidge_points->item(i,2)->text().toFloat();
            float upoint=ui->tableWidge_points->item(i,3)->text().toFloat();
            float spchange=((ui->tableWidge_points->item(i,4)->text()).toFloat());
            float points[4]={xpoint,ypoint,zpoint,upoint};
            //PMC_MovePara(Data::handle, 0, "SPEED", 0, spchangeList);
            PMC_MovePara(Data::handle,0,(char*)"SPEED",0,spchange);
            if(ui->radioButton_m->isChecked()){
                PMC_Move(Data::handle,4,axislist,points);
                PMC_MoveLimit(Data::handle,0,100);


            }
            else{
                PMC_MoveAbs(Data::handle,4,axislist,points);
            }
        }


    }


}

//停止
void MContinuityForm::on_PushButton_stop_clicked(){
    if(Data::handle!=nullptr){
        PMC_Rapidstop(Data::handle,3);
        ui->pushButton_start->setEnabled(true);
        runflage=false;
        ui->label_flag->setText("停止");
        ui->label_flag->setStyleSheet("background-color: rgb(255,0,0)");
    }

}

//坐标清零
void MContinuityForm::on_PushButton_cleanpos_clicked(){
    if(Data::handle!=nullptr){
        for(int i=0;i<4;++i){
            PMC_SetDpos(Data::handle,axislist[i],0);
        }
    }
}

//打开窗口事件
void MContinuityForm::showEvent(QShowEvent *event){
    ui->tableWidge_points->setRowCount(1);
    ui->tableWidge_points->setColumnCount(5);
    //ui->tableWidge_points->setTextElideMode(Qt::TextElideMode::ElideMiddle);
    ui->tableWidge_points->setColumnWidth(0,80);
    ui->tableWidge_points->setColumnWidth(1,80);
    ui->tableWidge_points->setColumnWidth(2,80);
    ui->tableWidge_points->setColumnWidth(3,80);
    ui->tableWidge_points->setColumnWidth(4,80);

    ui->tableWidge_points->setHorizontalHeaderLabels(QStringList()<<"X"<<"Y"<<"Z"<<"U"<<"速度");
    ui->tableWidge_points->setItem(0,0,new QTableWidgetItem("0"));
    ui->tableWidge_points->setItem(0,1,new QTableWidgetItem("0"));
    ui->tableWidge_points->setItem(0,2,new QTableWidgetItem("0"));
    ui->tableWidge_points->setItem(0,3,new QTableWidgetItem("0"));
    ui->tableWidge_points->setItem(0,4,new QTableWidgetItem("100"));

}



//添加点位
void MContinuityForm::on_addPoint_clicked(){
    pointrow++;
    ui->tableWidge_points->insertRow(pointrow);
    ui->tableWidge_points->setItem(pointrow,0,new QTableWidgetItem("0"));
    ui->tableWidge_points->setItem(pointrow,1,new QTableWidgetItem("0"));
    ui->tableWidge_points->setItem(pointrow,2,new QTableWidgetItem("0"));
    ui->tableWidge_points->setItem(pointrow,3,new QTableWidgetItem("0"));
    ui->tableWidge_points->setItem(pointrow,4,new QTableWidgetItem("100"));
}

//移除点位
void MContinuityForm::on_removePoint_clicked(){
    ui->tableWidge_points->removeRow(pointrow);
    pointrow--;
}


//报警信息
void MContinuityForm::showWarn(QString message){
    warn=QMessageBox::information(this,tr("warn"),message);
}


//窗口更新
void MContinuityForm::updateWin(){

    if(Data::handle!=nullptr){
        float dpos=0;
        for(int i=0;i<4;++i){
            PMC_GetDpos(Data::handle,axislist[i],&dpos);
            QString pos = QString::number( dpos,'f',3);
            if(i==0) ui->label_xpos->setText(pos);
            else if(i==1) ui->label_ypos->setText(pos);
            else if(i==2) ui->label_zpos->setText(pos);
            else if(i==3) ui->label_upos->setText(pos);
        }
        if(runflage){
            int value[4]={0,0,0,0};
            for(int i=0;i<4;++i){
                PMC_GetIfIdle(Data::handle,i,&value[i]);
            }
            //cout<<value[0]<<"_"<<value[1]<<"_"<<value[2]<<"_"<<value[3]<<endl;
            if(value[0]==-1&&value[1]==-1&&value[2]==-1&&value[3]==-1){
                runflage=false;
                ui->pushButton_start->setEnabled(true);
                ui->pushButton_start->setEnabled(true);
                ui->label_flag->setText("停止");
                ui->label_flag->setStyleSheet("background-color: rgb(255,0,0)");
            }

            PMC_GetVpSpeed(Data::handle, 0, &nowSpeed);
            ui->label_nowsp->setText(QString::number(nowSpeed));

            PMC_GetRemain_LineBuffer(Data::handle, 0, &rebuf);
            ui->label_residuem->setText(QString::number(rebuf));
            PMC_GetMoveCurmark(Data::handle, 0, &mark);
            ui->label_nowmark->setText(QString::number(mark));
        }
    }
}
